<?php
namespace app\admin\controller;
use app\admin\model\AdModel;
use think\Controller;

// 活动管理
class Team extends Controller{
	public function teamList(){
		if(request()->isAjax()){
            $return = db('team')-> select();
			if($return){
			 return json($return);
			}
		}else{
		// 渲染模板输出
		return $this->fetch('teamList');
		}
	}

	public function addTeam(){
		if(request()->isPost()){
		// 获取表单上传文件 例如上传了001.jpg
    		$file = request()->file("pic");

    	// 移动到框架应用根目录/uploads/ 目录下
    		$info = $file->move(ROOT_PATH . 'public' . DS . 'upload');
    		if($info){
		        // 拼接完全路径
		        $route = request()->domain()."/upload/".$info->getSaveName();
		        $data = input('post.');
		        $data["pic"] = $route;

		        $result = db('team')->insert($data);

		        if($result){
		        	$this->success('新增成功', 'addTeam','',2);
		        }
			}
		}

		return $this->fetch('addTeam');
	}	

	public function removeTeam(){
		// 接受 json 字符串
		$requestDataJson = $GLOBALS['HTTP_RAW_POST_DATA'];
		
		// json 字符串反序列化成数组
		$requestData = json_decode($requestDataJson,true);

		$result = db('team')->where('id')->delete($requestData["id"]);

		if($result){
			echo 1;
		}
	}
	public function editTeam(){
		$data = input('get.id');

		$formData = db('team')->where('id',$data)->select();

		$this->assign('formData',$formData);

		if(request()->isPost()){
		// 获取表单上传文件 例如上传了001.jpg
    		$file = request()->file("pic");

    		// 如果有上传图片
    		if($file){
    			// 移动到框架应用根目录/uploads/ 目录下
	    		$info = $file->move(ROOT_PATH . 'public' . DS . 'upload');

		        // 拼接完全路径
		        $route = request()->domain()."/upload/".$info->getSaveName();
		        $upData = input('post.');
		        $upData["pic"] = $route;

		        $result = db('team')->where('id',$upData["id"])->update($upData);

		        if(1){
		        	$this->success('修改成功', 'teamList','',2);
		        }

    		}else {

    			$upData = input('post.');
    			$result = db('team')->where('id',$upData["id"])->update($upData);
    			if(1){
		        	$this->success('修改成功', 'teamList','',2);
		        }
    		}
    		
		}

		return $this->fetch('editTeam');
	}
}